﻿using System.Linq;
using System.Web.Mvc;
using MemcachedProviders.Cache;
using ProjectYANApi.MemcachedFunction;
using ProjectYANApi.Models;
using YanOnline.Function;
using System.Collections.Generic;
using ProjectYANApi.Function;

namespace ProjectYANApi.Controllers
{
    public class ClearCacheController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CheckLogin(string lgUsername, string lgPassword)
        {
            string[,] array2D = new string[,] { 
                { "meocoi", "meocoi123@" }, 
                { "hahoang", "ha123456" },
                { "minhtung", "minhtungYan123@" }, 
                { "haily", "haily@12345" }, 
                { "admin", "cuibap123@" }, 
                { "videoteam", "videoteam@123" }, 
                { "haibang", "haibang123@" } };

            bool isOK = false;
            for (int i = 0; i < 7; i++)
            {
                if (lgUsername == array2D[i, 0] && lgPassword == array2D[i, 1])
                {
                    Session["user_clear_cache"] = lgUsername;
                    isOK = true;
                    clsFunction.SaveLogClearCache(lgUsername, "login clear cache MAPI success", "login clear cache MAPI success",
                        System.DateTime.Now.ToString(),
                        System.Web.HttpContext.Current.Request.UserHostName);
                    break;
                }
            }
            Session["user_clear_cache_result"] = isOK == true ? 1 : 0;

            return RedirectToAction("Index");
        }

        int PageSize = 30;
        public ActionResult ClearAll()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: ClearAll", "Xóa cache: ClearAll",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);

            //HOMEPAGE
            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetHomePage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetHomePage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            //TIN MỚI CẬP NHẬT
            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetNewNewsWithPage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetNewNewsWithPage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            //tin theo chuyen mục
            string[] ZoneIDs = { "4", "6", "2", "7", "9", "8", "11", "1" };
            for (int zoneId = 0; zoneId < ZoneIDs.Length; zoneId++)
            {
                for (int i = 1; i < 11; i++)
                {
                    string strId = MemcachedKey.hsApi_tblContentGetByZoneWithPage + "_" + i + "_" + zoneId;
                    List<ListContent> lst = new List<ListContent>();
                    lst = clsFunction.Api_tblContentGetByZoneWithPage(PageSize, i, zoneId);
                    DistCache.Remove(strId);
                    DistCache.Add(strId, lst);
                }
            }

            //tin tong hop
            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetEnterZoneWithPage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetEnterZoneWithPage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            return RedirectToAction("Index");
        }

        public ActionResult AppHomePage()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: AppHomePage", "Xóa cache: AppHomePage",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);

            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetHomePage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetHomePage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            return RedirectToAction("Index");
        }
        public ActionResult AppMlogPage()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: AppMlogPage", "Xóa cache: AppMlogPage",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);

            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetBlogZoneWithPage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetBlogZoneWithPage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            return RedirectToAction("Index");
        }
        public ActionResult AppSaoPage()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: AppSaoPage", "Xóa cache: AppSaoPage",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);

            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetCelebZoneWithPage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetCelebZoneWithPage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            return RedirectToAction("Index");
        }
        public ActionResult AppVideoPage()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: AppVideoPage", "Xóa cache: AppVideoPage",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);

            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetVideoZoneWithPage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetVideoZoneWithPage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            return RedirectToAction("Index");
        }
        public ActionResult AppNewNewsPage()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: AppNewNewsPage", "Xóa cache: AppNewNewsPage",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);

            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetNewNewsWithPage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetNewNewsWithPage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            return RedirectToAction("Index");
        }
        public ActionResult AppNewGetByZone()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: AppNewGetByZone", "Xóa cache: AppNewGetByZone",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);
            string[] ZoneIDs = { "4", "6", "2", "7", "9", "8", "11", "1" };
            for (int zoneId = 0; zoneId < ZoneIDs.Length; zoneId++)
            {
                for (int i = 1; i < 11; i++)
                {
                    string strId = MemcachedKey.hsApi_tblContentGetByZoneWithPage + "_" + i + "_" + zoneId;
                    List<ListContent> lst = new List<ListContent>();
                    lst = clsFunction.Api_tblContentGetByZoneWithPage(PageSize, i, zoneId);
                    DistCache.Remove(strId);
                    DistCache.Add(strId, lst);
                }
            }

            return RedirectToAction("Index");
        }
        public ActionResult AppEnterZone()
        {
            clsFunction.SaveLogClearCache(Session["user_clear_cache"].ToString(), "Xóa cache: AppEnterZone", "Xóa cache: AppEnterZone",
                System.DateTime.Now.ToString(),
                System.Web.HttpContext.Current.Request.UserHostName);

            for (int i = 1; i < 11; i++)
            {
                string strId = MemcachedKey.hstblContentGetEnterZoneWithPage + "_" + i;
                List<ListContent> lst = new List<ListContent>();
                lst = clsFunction.Api_tblContentGetEnterZoneWithPage(PageSize, i);
                DistCache.Remove(strId);
                DistCache.Add(strId, lst);
            }

            return RedirectToAction("Index");
        }
    }
}
